package com.tk.exam.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.Locale;
import java.util.Objects;

/**
 * @author gaowei
 * @Description: 日期时间转换工具类
 * @date 2018/03/13 22:32
 */
public class DateUtils {

    public static Date parseStringTimesToDate(String stringTime){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//Date指定格式：yyyy-MM-dd HH:mm:ss
        Date date=null;
        try {
            date=simpleDateFormat.parse(stringTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static String parseTimesTampString(String strDate) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Long time = new Long(strDate);
        return format.format(time);
    }

    public static String parseTimesTampDate(Date date) {
        if(Objects.isNull(date))
            return "";
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sf.format(date);
    }

    public static String parseTimesTampDateV1(Date date) {
        if(Objects.nonNull(date)){
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            return sf.format(date);
        }else{
            return "";
        }
    }

    public static String dealDateFormat(String oldDateStr) {
        Date date1 = null;
        DateFormat df2 = null;
        try {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
            Date date = df.parse(oldDateStr);
            SimpleDateFormat df1 = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK);
            date1 = df1.parse(date.toString());
            df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return df2.format(date1);
    }

    /**
     * 获取当前时间
     * @return
     */
    public static Date now(){
        ZoneId zone = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.now();
        Date now = Date.from(localDateTime.atZone(zone).toInstant());
        return now;
    }


    /**
     * 将分钟转化为毫秒数
     * @param minute
     * @return
     */
    public static Long minuteToMillisecond(Integer minute){
        return minute * 60 * 1000L;
    }

    /**
     * 获取两个时间所差的分钟数
     * @param startTime
     * @param endTime
     * @return
     */
    public static Integer getTimeDifference2Minute(Date startTime, Date endTime) {
        return Integer.parseInt((endTime.getTime() - startTime.getTime())/(1000*60)+"");
    }

    /**
     * 获取两个时间所差的秒数
     * @param startTime
     * @param endTime
     * @return
     */
    public static Long getTimeDifference2Second(Date startTime, Date endTime) {
        return (endTime.getTime() - startTime.getTime())/1000;
    }


    /**
     * 获取两个时间所差的毫秒数
     * @param startTime
     * @param endTime
     * @return
     */
    public static Long getTimeDifference2Millisecond(Date startTime, Date endTime) {
        return (endTime.getTime() - startTime.getTime());
    }



    /**
     * 根据开始时间和时间长度 转化成时间区间
     * @param examTime          2019-06-20 12：00
     * @param examTotalTime     120
     * @return                  2019-06-20 12：00—14:00
     */
    public static String toTimeInterval(Date examTime, Integer examTotalTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

        String examStartTime = sdf.format(examTime);
        String examEndTime = sdf.format(toNewTime(examTime, examTotalTime));
        String result = examStartTime + "—" + examEndTime.substring(examEndTime.length()-5);
        return result;
    }

    /**
     * 根据开始时间和时间长度 转化成新的时间
     * @param examTime
     * @param examTotalTime
     * @return
     */
    public static Date toNewTime(Date examTime, Integer examTotalTime) {
        return new Date(examTime.getTime() + (examTotalTime*60*1000));
    }

    /**
     * 将时间转换成 yyyy-MM-dd HH:mm:ss 格式
     * @param examTime
     * @return
     */
    public static String formate(Date examTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = sdf.format(examTime);
        return time;
    }
}